import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import { Message } from "element-ui";//提示信息
import { getToken } from "@/utils/TokenTools";
Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'Login',
    //这是一个懒加载
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/Login.vue')
  },
  {
    path: '/',
    name: 'Home',
    component: Home,
    meta: {
      islogin: true,
    }
  },
  {
    path: '/about',
    name: 'About',
    //这是一个懒加载
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue'),
  },
  {
    path: '/user',
    name: 'User',
    //这是一个懒加载
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/User.vue'),
    meta: {
      islogin: true,
    }
  }, {
    path: '/video',
    name: 'Video',
    //这是一个懒加载
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/Video.vue'),
    meta: {
      islogin: true,
    }
  }, {
    path: '/addVideo',
    name: 'addVideo',
    //这是一个懒加载
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AddVideo.vue'),
    meta: {
      islogin: true,
    }
  },
  {
    path: '/editVideo',
    name: 'EditVideo',
    component: () => import('../views/EditVideo.vue'),
    meta: {
      islogin: true,
    }
  },
  {
    path: '/search',
    name: 'Search',
    //这是一个懒加载
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/Search.vue'),
    meta: {
      islogin: true,
    }
  }
  , {
    path: '*',
    name: '404',
    component: () => import('../views/404.vue')
  }

]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  if (to.meta.islogin) {

    if (getToken()) {
      next();
    } else {
      next({ name: 'Login' });
      console.log(998);
      Message.error("请登录");
    }

  }
  next();
})
export default router
